package com.hcc.gateway.netty.route;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.map.MapUtil;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 路由缓存
 *
 * @author shengjun.hu
 * @date 2021/12/29
 */
public class GatewayRouteCache {

    private static final String CACHE_KEY = "gatewayRoute";
    private static final Cache<String, Map<String, RouteDefinition>> ROUTE_CACHE = CacheUtil.newLFUCache(Integer.MAX_VALUE);

    public static void put(RouteDefinition routeDefinition) {
        Map<String, RouteDefinition> cache = ROUTE_CACHE.get(CACHE_KEY);
        if (MapUtil.isEmpty(cache)) {
            cache = new ConcurrentHashMap<>();
        }
        cache.put(routeDefinition.getId(), routeDefinition);

        ROUTE_CACHE.put(CACHE_KEY, cache);
    }

    public static Map<String, RouteDefinition> getCache() {
        return ROUTE_CACHE.get(CACHE_KEY);
    }

}
